﻿#include "userdialog.h"
#include "ui_userdialog.h"

UserDialog::UserDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserDialog)
{
    ui->setupUi(this);
    initRole();

    connect(ui->m_btnCancel, &QPushButton::clicked, this, &UserDialog::on_cancel);
    connect(ui->m_btnSubmit, &QPushButton::clicked, this, &UserDialog::on_submit);
}

UserDialog::~UserDialog()
{
    delete ui;
}

void UserDialog::init(User user)
{
    ui->m_username->setText(user.userName);
    ui->m_pwd->setText(user.pwd);
    int index = ui->m_roleid->findData(user.roleId);
    if(index >= 0)
    {
        ui->m_roleid->setCurrentIndex(index);
    }
    m_user = user;
}

void UserDialog::initRole()
{
    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if (database.open())
    {
        QSqlQuery sql_query(database);
        QString select_sql = "select role_id, role_name from t_role";
        if(!sql_query.exec(select_sql))
        {
            qDebug() << sql_query.lastError().text();
        }
        else
        {
            while(sql_query.next())
            {
                int roleId = sql_query.value(0).toInt();
                QString roleName = sql_query.value(1).toString();
                ui->m_roleid->addItem( roleName, roleId);
            }
        }
    }
}

void UserDialog::on_cancel()
{
    reject();
}

void UserDialog::on_submit()
{
    if(QMessageBox::question(this,
                             QString::fromLocal8Bit("提示"),
                             QString::fromLocal8Bit("请确认操作"),
                             QString::fromLocal8Bit("取消"),
                             QString::fromLocal8Bit("确认")) != 1)
    {
        return;
    }

    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if (database.open())
    {
        QSqlQuery sql_query(database);
        if(m_user.op == "ADD")
        {
            QString sql = QString("insert into t_user(user_name, pwd, role_id) "
                                  "values ('%1', '%2', %3)")
                    .arg(ui->m_username->text())
                    .arg(ui->m_pwd->text())
                    .arg(ui->m_roleid->currentData().toLongLong());
            if(sql_query.exec(sql))
            {
                qDebug() << sql;
                qDebug() << "insert t_user " << ui->m_username->text();
            }
        }

        if(m_user.op == "EDIT")
        {
            QString sql = QString("update t_user "
                                  "set user_name = '%1',"
                                  "pwd = '%2',"
                                  "role_id = %3"
                                  " where user_id = %4")
                    .arg(ui->m_username->text())
                    .arg(ui->m_pwd->text())
                    .arg(ui->m_roleid->currentData().toLongLong())
                    .arg(m_user.userId);
            if(sql_query.exec(sql))
            {
                qDebug() << sql;
                qDebug() << "update t_user " << ui->m_username->text();
            }
        }
    }

    accept();
}
